package com.jereh.topnews.activity;

import android.app.AlertDialog;
import android.app.Dialog;
import android.content.DialogInterface;
import android.graphics.drawable.BitmapDrawable;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.webkit.WebView;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.Toast;

import com.jereh.topnews.R;
import com.jereh.topnews.entity.Content;
import com.jereh.topnews.util.WebUtil;


/**
 * 新闻详细页
 */
public class NewsDetailActivity extends AppCompatActivity implements View.OnClickListener{

    private Content content;//新闻内容
    private WebView webView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_news_detail);
        content=(Content)getIntent().getSerializableExtra("content");
        webView=(WebView)findViewById(R.id.webView);
        initClick();
        webView.loadData(WebUtil.setImgAutoFit(content.getHtml()),"text/html;charset=UTF-8",null);
    }

    private void initClick(){
        findViewById(R.id.leftBtn).setOnClickListener(this);
        findViewById(R.id.collect).setOnClickListener(this);
        findViewById(R.id.share).setOnClickListener(this);
        findViewById(R.id.rightBtn).setOnClickListener(this);
        findViewById(R.id.reply).setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.leftBtn:
                finish();
                break;
            case R.id.collect:
                Toast toast=Toast.makeText(this,"",Toast.LENGTH_LONG);
                //设置文字
                toast.setText("收藏成功");
                //设置显示时间
                toast.setDuration(Toast.LENGTH_LONG);
                int offsetX=0;
                int offsetY=0;
                LinearLayout ll= (LinearLayout)LayoutInflater.from(this).inflate(R.layout.toast_view, null, false);
                //设置背景面板
                toast.setView(ll);
                //设置位置
                toast.setGravity(Gravity.CENTER, offsetX, offsetY);
                //展示提示信息
                toast.show();
                break;
            case R.id.share:
                Dialog dialog=new AlertDialog.Builder(this)
                        .setTitle("请选择您想转发的平台")
                        .setIcon(R.mipmap.icon)
                        .setMessage("在这里您可以选择想要转发的平台应用")
                        .setPositiveButton("确定", new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                //隐藏dialog
                                dialog.dismiss();
                            }
                        })
                        .create();
                dialog.show();
                break;
            case R.id.rightBtn:
                final AlertDialog alertDialog=new AlertDialog.Builder(this).create();
                alertDialog.show();
                //面板的内容
                Window window=alertDialog.getWindow();
                window.setContentView(R.layout.dialog_share_panel);
                window.findViewById(R.id.wxpyq).setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        Toast.makeText(NewsDetailActivity.this, "恭喜您已转发至微信朋友圈", Toast.LENGTH_SHORT).show();
                        alertDialog.dismiss();
                    }
                });
                window.findViewById(R.id.cancel).setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        alertDialog.dismiss();
                    }
                });
                break;
            case R.id.reply:
                //弹出一个popupWindow
                //给popup制定一个布局
                View view=getLayoutInflater().inflate(R.layout.dialog_share_panel,null);
                final PopupWindow popup=new PopupWindow(view, ViewGroup.LayoutParams.MATCH_PARENT,600);
//                popup.setFocusable(true);

                //设置popupwindow点击空白区域隐藏的效果
                popup.setBackgroundDrawable(new BitmapDrawable());
                popup.setOutsideTouchable(true);//不能在没有焦点的时候使用 

                view.findViewById(R.id.wxpyq).setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        Toast.makeText(NewsDetailActivity.this, "恭喜您已分享到朋友圈", Toast.LENGTH_SHORT).show();
                        //把窗口关闭
                        popup.dismiss();
                    }
                });
                view.findViewById(R.id.cancel).setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        popup.dismiss();
                    }
                });
                popup.showAsDropDown(findViewById(R.id.rightBtn));
//                popup.showAsDropDown(findViewById(R.id.rightBtn),0,50);
//                popup.showAsDropDown(findViewById(R.id.bottomPanel),0,50,Gravity.TOP);
//                popup.showAtLocation(findViewById(R.id.rightBtn),Gravity.TOP,0,500);
                break;
        }
    }
}
